package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusDataSource;
import com.zhongwang.business.gen.entity.BusMaterialData;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusMaterialDataModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AluminiumModel;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.XYHFileUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 徐一贺
 * @className RawMatImportListener
 * @description 铝材信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AluminiumImportListener extends AnalysisEventListener<AluminiumModel> {
    /**
     * 钩子
     */
    private ImportCallBack callBack;
    /**
     * Excel相关参数模型
     */
    private AluminiumModel param;
    /**
     * 材料信息数据持久层
     */
    private AccountingInfoDao accountingInfoDao;
    /**
     * 错误列表
     */
    private List<AluminiumModel> errorList = new ArrayList<>();
    /**
     * 可用数据列表
     */
    private List<BusMaterialData> rawMatList = new ArrayList<>();
    /**
     * 当前时间
     */
    private Date currentTime = new Date();

    /**
     * 构造函数
     *
     * @param param             页面参数
     * @param callBack          回调函数
     * @param accountingInfoDao 持久层
     */
    public AluminiumImportListener(AluminiumModel param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
        this.callBack = callBack;
        this.param = param;
        this.accountingInfoDao = accountingInfoDao;
    }

    /**
     * 验证
     *
     * @param data 每行数据
     */
    @Override
    public void invoke(AluminiumModel data, AnalysisContext context) {
        BusBudgetAccountingCost cost = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(param.getId());
        StringBuffer warningMessage = new StringBuffer();
        if (data.getPart() != null && !data.getPart().equals("")) {
            data.setPart(data.getPart().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getPart() == null) {
            warningMessage.append("请填写部分！正确部分是" + cost.getPart() + "。\r\n");
        } else if ("".equals(data.getPart())) {
            warningMessage.append("请填写部分！正确部分是" + cost.getPart() + "。\r\n");
        } else if (!data.getPart().equals(cost.getPart())) {
            warningMessage.append("部分不正确！请修改正确后重新导入！正确部分是" + cost.getPart() + "。\r\n");
        }
        if (data.getMajor() != null && !data.getMajor().equals("")) {
            data.setMajor(data.getMajor().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getMajor() == null) {
            warningMessage.append("请填写专业！正确专业是" + cost.getMajor() + "。\r\n");
        } else if ("".equals(data.getMajor())) {
            warningMessage.append("请填写专业！正确专业是" + cost.getMajor() + "。\r\n");
        } else if (!data.getMajor().equals(XYHFileUtils.replaceBlank(cost.getMajor()))) {
            warningMessage.append("专业不正确！请修改正确后重新导入！正确专业是" + cost.getPart() + "。\r\n");
        }
        if (data.getMatDesc() != null && !data.getMatDesc().equals("")) {
            data.setMatDesc(data.getMatDesc().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getMatDesc() == null) {
            warningMessage.append("请填写提报计划物料描述！\r\n");
        } else if ("".equals(data.getMatDesc())) {
            warningMessage.append("请填写提报计划物料描述！\r\n");
        }
        if (data.getTexture() != null && !data.getTexture().equals("")) {
            data.setTexture(data.getTexture().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getTexture() == null) {
            warningMessage.append("请填写材质！正确材质是铝材。\r\n");
        } else if ("".equals(data.getTexture())) {
            warningMessage.append("请填写材质！正确材质是铝材。\r\n");
        } else if (!"铝材".equals(data.getTexture())) {
            warningMessage.append("材质不正确！请修改正确后重新导入！正确材质是铝材。\r\n");
        }
        if (data.getNum() != null && !data.getNum().equals("")) {
            data.setNum(data.getNum().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getNum() == null) {
            warningMessage.append("请填写理论总重！\r\n");
        } else if ("".equals(data.getNum())) {
            warningMessage.append("请填写理论总重！\r\n");
        } else {
            try {
                BigDecimal num = new BigDecimal(data.getNum());
                //小于等于-1
                if (num.compareTo(new BigDecimal(-1)) < 1) {
                    warningMessage.append("理论总重不正确！请修改正确后重新导入！正确的实用数量应该是非负数字。\r\n");
                }
            } catch (Exception e) {
                warningMessage.append("理论总重不正确！请修改正确后重新导入！正确的实用数量应该是非负数字。\r\n");
            }
        }
        if (data.getUnit() != null && !data.getUnit().equals("")) {
            data.setUnit(data.getUnit().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getUnit() == null) {
            warningMessage.append("请填写单位！\r\n");
        } else if ("".equals(data.getUnit())) {
            warningMessage.append("请填写单位！\r\n");
        } else if (!"KG".equals(data.getUnit())) {
            warningMessage.append("单位不正确！请求该正确后重新导入！正确的单位是KG（大写）！\r\n");
        }
        if (data.getSapCode() != null && !data.getSapCode().equals("")) {
            data.setSapCode(data.getSapCode().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getBuyMatDesc() != null && !data.getBuyMatDesc().equals("")) {
            data.setBuyMatDesc(data.getBuyMatDesc().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getPrice() != null && !data.getPrice().equals("")) {
            data.setPrice(data.getPrice().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getPrice() == null) {
            warningMessage.append("请填写单价！\r\n");
        } else if ("".equals(data.getPrice())) {
            warningMessage.append("请填写单价！\r\n");
        } else {
            try {
                BigDecimal price = new BigDecimal(data.getPrice());
                //小于等于-1
                if (price.compareTo(new BigDecimal(-1)) < 1) {
                    warningMessage.append("单价不正确！请修改正确后重新导入！正确的单价应该是非负数字。\r\n");
                }
            } catch (Exception e) {
                warningMessage.append("单价不正确！请修改正确后重新导入！正确的单价应该是非负数字。\r\n");
            }
        }
        if (warningMessage == null || warningMessage.toString().length() == 0) {
            //获取对应的数据源
            BusDataSource condition = new BusDataSource();
            condition.setBatId(param.getBatId());
            condition.setMajor(param.getMajor());
            condition.setPart(param.getPart());
            condition.setDataCode("ds003");
            BusDataSource dataSource = accountingInfoDao.getBusDataSourceByCondition(condition);
            data.setDsId(dataSource.getId());
            data.setBatId(param.getBatId());
            data.setMajor(param.getMajor());
            data.setPart(param.getPart());
            data.setPriceType(2);
            data.setDataUseType(2);
            data.setBuyType("原材料");
            data.setProCode(param.getProCode());
            data.setGetDate(currentTime);
            data.setGetUid(ShiroUtils.getUserId());
            rawMatList.add(rawMatModelToBusMaterialData(data));
        } else {
            data.setWarnMessage(warningMessage.toString());
            errorList.add(data);
        }
    }

    /**
     * 持久化
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (errorList.size() > 0) {
            callBack.onWarning(errorList);
        } else if (rawMatList.size() > 0) {
            //获取对应的数据源
            BusMaterialDataModel rawMatCondition = new BusMaterialDataModel();
            //获取对应的数据源
            BusDataSource condition = new BusDataSource();
            condition.setBatId(param.getBatId());
            condition.setMajor(param.getMajor());
            condition.setPart(param.getPart());
            condition.setDataCode("ds003");
            BusDataSource dataSource = accountingInfoDao.getBusDataSourceByCondition(condition);
            rawMatCondition.setBatId(param.getBatId());
            rawMatCondition.setMajor(param.getMajor());
            rawMatCondition.setPart(param.getPart());
            rawMatCondition.setDataUseType(2);
            rawMatCondition.setDsId(dataSource.getId());
            rawMatCondition.setBuyType("原材料");
            rawMatCondition.setTexture("铝材");
            accountingInfoDao.deleteBusMaterialDataByRawMatCondition(rawMatCondition);
            for (int i = 0; i < rawMatList.size(); i++) {
                accountingInfoDao.insertBusMaterialData(rawMatList.get(i));
            }
            BusBudgetAccountingCost cost = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(param.getId());
            cost.setDataSource("1");
            accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(cost);
            callBack.onSuccess();
        }
    }

    /**
     * 异常处理
     *
     * @param exception 异常信息
     * @param context
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) {

    }

    /**
     * rawMatModelToBusMaterialData 模型转换类
     *
     * @param
     * @return com.zhongwang.business.gen.entity.BusMaterialData
     * @throws
     * @author 徐一贺
     * @date 2020/11/21 15:09
     */
    private BusMaterialData rawMatModelToBusMaterialData(AluminiumModel source) {
        BusMaterialData busMaterialData = new BusMaterialData();
        busMaterialData.setId(source.getId());
        busMaterialData.setResultId(null);
        busMaterialData.setRppId(null);
        busMaterialData.setBatId(source.getBatId());
        busMaterialData.setMatDesc(source.getMatDesc());
        busMaterialData.setNum(new BigDecimal(source.getNum()));
        busMaterialData.setUnit(source.getUnit());
        busMaterialData.setStandardNo(source.getStandardNo());
        busMaterialData.setBrand(source.getBrand());
        busMaterialData.setRemark(source.getRemark());
        busMaterialData.setDiagram(source.getDiagram());
        busMaterialData.setAssistType(source.getAssistType());
        busMaterialData.setPart(source.getPart());
        busMaterialData.setMajor(XYHFileUtils.replaceBlank(source.getMajor()));
        busMaterialData.setBuyType(source.getBuyType());
        busMaterialData.setMalType(source.getMalType());
        busMaterialData.setPrice(new BigDecimal(source.getPrice()));
        busMaterialData.setTexture(XYHFileUtils.replaceBlank(source.getTexture()));
        busMaterialData.setSapCode(source.getSapCode());
        busMaterialData.setDsId(source.getDsId());
        busMaterialData.setUseType(source.getUseType());
        busMaterialData.setProCode(source.getProCode());
        busMaterialData.setBuyMatDesc(source.getBuyMatDesc());
        busMaterialData.setBuyNum(source.getBuyNum());
        busMaterialData.setBuyUnit(source.getBuyUnit());
        busMaterialData.setBuyStandardNo(source.getBuyStandardNo());
        busMaterialData.setBuyBrand(source.getBuyBrand());
        busMaterialData.setPriceType(source.getPriceType());
        busMaterialData.setDataType(source.getDataType());
        busMaterialData.setDataUseType(source.getDataUseType());
        busMaterialData.setSourceProCode(source.getSourceProCode());
        busMaterialData.setSourcePart(source.getSourcePart());
        busMaterialData.setGetDate(source.getGetDate());
        busMaterialData.setGetUid(source.getGetUid());
        busMaterialData.setPostName(source.getPostName());
        busMaterialData.setPostTel(source.getPostTel());
        busMaterialData.setPostDept(source.getPostDept());
        return busMaterialData;
    }
}
